package com.fireXXX.job.analysis.crawler.boss;

import com.fireXXX.job.analysis.crawler.boss.constant.CityTypeEnum;
import com.fireXXX.job.analysis.crawler.boss.constant.TechnicalTypeEnum;
import com.fireXXX.job.analysis.crawler.boss.data.OriginCompany;
import com.fireXXX.job.analysis.crawler.boss.data.OriginJobItem;
import com.fireXXX.job.analysis.crawler.common.Page;
import com.fireXXX.job.analysis.crawler.error.TaskTypeEndCrawlerException;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class BossJobParser {

    private OriginJobItem parseOne(CityTypeEnum city, TechnicalTypeEnum tech,
                                   Element job) {
        //招聘概要信息
        OriginJobItem workInfo = new OriginJobItem();
        String jobName = job.getElementsByClass("job-name").get(0).text();
        String jobArea = job.getElementsByClass("job-area-wrapper").get(0).text();
        String jobPubTime = job.getElementsByClass("job-pub-time").get(0).text();

        String jobSalary = job.getElementsByClass("job-limit").get(0).getElementsByClass("red").text();
        String educationWork = job.getElementsByClass("job-limit").get(0).getElementsByTag("p").get(0).outerHtml();
        String education = educationWork.substring(educationWork.lastIndexOf("</em>")+5,educationWork.indexOf("</p>"));
        String jobLimit = job.getElementsByClass("job-limit").get(0).getElementsByTag("p").get(0).text().replace(education,"");
        String recruitersNameJob = job.getElementsByClass("info-publis").get(0).getElementsByClass("name").get(0).outerHtml();
        String recruitersJob = recruitersNameJob.substring(recruitersNameJob.lastIndexOf("</em>")+5,recruitersNameJob.indexOf("</h3>"));
        String recruitersName = job.getElementsByClass("info-publis").get(0).getElementsByClass("name").text().replace(recruitersJob, "");
        String url = "https://www.zhipin.com"+job.getElementsByClass("primary-box").get(0).attr("href");

        List<String> tagList = new LinkedList<>();
        Elements tags = job.getElementsByClass("tag-item");
        for (Element tag : tags) {
            tagList.add(tag.text());
        }

        //公司信息
        OriginCompany company = new OriginCompany();
        String companyName = job.getElementsByClass("company-text").get(0).getElementsByTag("h3").text();
        String companyType = job.getElementsByClass("company-text").get(0).getElementsByTag("p").get(0).getElementsByTag("a").text();
        String companyInfo = job.getElementsByClass("company-text").get(0).getElementsByTag("p").get(0).outerHtml();
        String companyPublicStep = null;
        try {
            companyPublicStep = companyInfo.substring(companyInfo.indexOf("</em>")+5,companyInfo.lastIndexOf("<em"));
        } catch (Exception e) {
            companyPublicStep = "";
        }
        String companyEmployeeNumber = companyInfo.substring(companyInfo.lastIndexOf("</em>")+5,companyInfo.indexOf("</p>"));
        String benefits = job.getElementsByClass("info-desc").get(0).text();

        company.setName(companyName);
        company.setIndustry(companyType);
        company.setPublicStep(companyPublicStep);
        company.setEmployeeNumber(companyEmployeeNumber);
        company.setBenefits(Arrays.asList(benefits.split("，")));

        workInfo.setName(jobName);
        workInfo.setArea(jobArea);
        workInfo.setPublishTime(jobPubTime);
        workInfo.setEducation(education);
        workInfo.setExperiences(jobLimit);
        workInfo.setTags(tagList);
        workInfo.setDetailUrl(url);
        workInfo.setSalary(jobSalary);
        workInfo.setRecruitersName(recruitersName);
        workInfo.setRecruitersJob(recruitersJob);
        workInfo.setCompany(company);

        return workInfo;
    }

    public List<OriginJobItem> parse(CityTypeEnum city, TechnicalTypeEnum tech, Page page,
                                     String content) {
        List<OriginJobItem> originJobItems = new ArrayList<>();

        Document document = Jsoup.parseBodyFragment(content);
        //获取body
        Element body = document.body();

        Elements jobPrimaries = null;
        try {
            jobPrimaries = body.getElementById("main").getElementsByClass("job-primary");
        } catch (IndexOutOfBoundsException exception) {
            throw new TaskTypeEndCrawlerException();
        }

        if (jobPrimaries == null || jobPrimaries.size() == 0) {
            throw new TaskTypeEndCrawlerException();
        }

        //遍历所有的招聘信息
        for (int i = 0; i < jobPrimaries.size(); i++) {
            //第i条招聘信息
            Element job = jobPrimaries.get(i);

            OriginJobItem item = null;
            try {
                item = parseOne(city, tech, job);
            } catch (Exception e) {
                log.error("错误任务:" + job.html());
                e.printStackTrace();
            }

            if (item != null) {
                originJobItems.add(item);
            }
        }

        return originJobItems;
    }

}
